package com.pb.infra.auth.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pb.infra.auth.mapper.PriviResAuthMapper;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.service.SequenceGeneratorService;
import com.pb.infra.oth.util.DateTimeUtil;
import com.pb.infra.oth.util.JwtTokenConfig;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源管理
 * @author liudebao
 * @date 2020/11/30
 */
@Service
public class PriviResAuthService {

    /**
     * 多表DAO映射
     */
    @Resource
    private PriviResAuthMapper priviResAuthMapper;

    /**
     * 序号自动生成逻辑层
     */
    @Resource
    private SequenceGeneratorService sequenceGeneratorService;

    /**
     * jwttoken配置类
     */
    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 读取资源信息
     * @param map 请求参数
     * @return Result 返回数据
     */
    public Result getResData(Map<String, Object> map, Page page) {
        if (page != null && page.getPageIndex() != null && page.getPageSize() != null) {
            PageHelper.startPage(page.getPageIndex(), page.getPageSize());
        }
        List<Map<String, Object>> list = priviResAuthMapper.getResData(map);
        return Result.ok(new PageInfo(list).getTotal(), list);
    }

    /**
     * 新建资源信息
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @Transactional
    public Result insert(Map<String, Object> paramMap, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        paramMap.put("createUser", userId);
        paramMap.put("createTime", DateTimeUtil.getDate());
        paramMap.put("updateUser", userId);
        paramMap.put("updateTime", DateTimeUtil.getDate());
        Map<String, Object> map = new HashMap<>();
        if ("1".equals(paramMap.get("resType").toString())) {
            paramMap.put("resId", sequenceGeneratorService.getNextIdStr("INTER", "INTER"));
        } else {
            String seqId = sequenceGeneratorService.getNextIdStr("BTN", "BTN");
            map.put("seqId", seqId);
            paramMap.put("resId", seqId);
            priviResAuthMapper.addRoleRes(paramMap);
        }
        priviResAuthMapper.addResData(paramMap);
        return Result.ok(map);
    }

    /**
     * 修改资源信息
     * @param paramMap 请求参数
     * @param request
     * @return
     */
    @Transactional
    public void update(Map<String, Object> paramMap, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        Claims claims = jwtTokenConfig.getClaimByToken(token);
        String userId = (String)claims.get(jwtTokenConfig.getSub());
        paramMap.put("updateUser", userId);
        paramMap.put("updateTime", DateTimeUtil.getDate());
        priviResAuthMapper.editResData(paramMap);
    }

    @Transactional
    public void delRes(List<String> map) {
        for (String resId : map) {
            priviResAuthMapper.delResData(resId);
            priviResAuthMapper.delInterData(resId);
            priviResAuthMapper.delRoleRes(resId);
        }
    }
}
